﻿//完成与功能层的对接

#pragma once

#include <QMainWindow>
#include "ui_MemsAutoTestSoftware.h"
#include "qevent.h"
#include "qfiledialog.h"
#include "ACT/TurnTable/TurnTableCodec.h"
#include "ACT/MEMSData/MEMSDataCodec.h"
#include "ACT/MEMSData/MEMSDataHandler.h"
#include "ACT/MEMSData/MEMSDataSaver.h"
#include "ObjAutoInject.h"
#include "ACT/LogSaver.h"
class TurnTableRunningStateManager;
class ScriptRunner;
class ScriptSaveLoader;
class SerialPortManager;

class SetTurnTableAndThermostatDialog;
class SetMEMSDataDialog;
class SetScriptDialog;
class ParameterConsole;

class MemsAutoTestSoftware : public QMainWindow, public ObjAutoInject
{
	Q_OBJECT

private:

	TurnTableRunningStateManager* turnTableRunningStateManager = Q_NULLPTR;
	TurnTableCodec* turnTableCodec = Q_NULLPTR;

	MEMSDataCodec* mEMSDataCodec = Q_NULLPTR;
	MEMSDataHandler* mEMSDataHandler = Q_NULLPTR;
	MEMSDataSaver* mEMSDataSaver = Q_NULLPTR;
	ScriptRunner* scriptRunner = Q_NULLPTR;
	ScriptSaveLoader* scriptSaveLoader = Q_NULLPTR;
	LogSaver* logSaver = LogSaver::getLogSaver();

	SerialPortManager* serialPortManager_turnTable = Q_NULLPTR;
	SerialPortManager* serialPortManager_mEMSData = Q_NULLPTR;

	//ui对象
	Ui::MemsAutoTestSoftware ui;
	SetTurnTableAndThermostatDialog* setTurnTableAndThermostatDialog = Q_NULLPTR;
	SetMEMSDataDialog* setMEMSDataDialog = Q_NULLPTR;
	SetScriptDialog* setScriptDialog = Q_NULLPTR;
	ParameterConsole* parameterConsole = Q_NULLPTR;

	int accuracy = 4;
public:
	explicit MemsAutoTestSoftware(QWidget* parent = Q_NULLPTR);
	virtual ~MemsAutoTestSoftware();

	//对应每个按钮按下信号的槽，部分按钮逻辑较为简单，直接采用了lambda表达式
	private	slots:
	//文件
	void slot_file_loadScript();//加载脚本
	void slot_file_saveScript();//保存脚本
	void slot_file_about();//关于本软件
	//控制
	void slot_control_turnTable_connect();
	void slot_control_turnTable_disconnect();
	void slot_control_turnTable_run();//运行转台
	void slot_control_thermostat_run();//运行温箱
	void slot_control_dataReader_connect();
	void slot_control_dataReader_disconnect();
	//设置

	//对应各种反馈
	private  slots:
	//接收转台反馈
	void slot_updateTurnTableConnectionState(TurnTableCodec::ConnectionState connectionState);
	void slot_updateTurnTableCurrentState(float position, float rate);
	void slot_updateCurrentThermostatTemperature(float temperature);
	void slot_updateTurnTablePowerSupplyState(TurnTableCodec::TurnTablePowerSupplyState powerSupplyState);
	void slot_updateTurnTableCricuitState(TurnTableCodec::TurnTableCircuitState circuitState);
	void slot_updateTurnTableAmplifierState(TurnTableCodec::TurnTableAmplifierState amplifierState);
	void slot_updateTurnTableRunningState(TurnTableCodec::TurnTableRunningState runningState);
	void slot_turnTableErrorEcho(TurnTableCodec::ErrorFlag errorFlag);

	void slot_instructionEcho(TurnTableCodec::TurnTableInstructionFlag instructionFlag, float arg1, float arg2, float arg3);
	void slot_turnTablePositionModeEcho(float position, float rate, float acceleration);
	void slot_turnTableRateModeEcho(float rate, float acceleration);
	void slot_thermostatConnected();
	void slot_thermostatDisconnected();
	void slot_thermostatTargetTemperatureEcho(float temperature);
	void slot_thermostatTemperatureRateEcho(float temperatureRate);

	//脚本管理器反馈
	void slot_scriptIsStarted();
	void slot_scriptIsStoped();

	//接收MEMS数据编码解码器反馈
	void slot_mEMSDataCurrentDataPackageype(MEMSDataCodec::DataPackagetype dataPackagetype);

	//接收数据采集器反馈
	void slot_mEMSDataCollectionState(bool collectionState);
	void slot_mEMSDataCollectionProtocol(MEMSDataHandler::DataCollectionProtocol dataCollectionProtocol);

	//接收MEMS数据保存器的反馈
	void slot_saveDataFileFailed(QString filePath);
	void slot_mEMSDataToSave(int dataGroupIndex, QString data);

	//接收脚本保存加载器的反馈
	void slot_loadScriptFileFailed(QString filePath);
	void slot_saveScriptFileFailed(QString filePath);

	void initSignalsAndSlots();

private:
	void showMessage(QString msg);

	//代码优化,模块化
private:
	void initAllUI();

	//事件处理
private:
	void closeEvent(QCloseEvent* event);
public:
	int getAccuracy();
	void setAccuracy(int accuracy);
	//geter and seter
public:
	TurnTableRunningStateManager* getTurnTableRunningStateManager();
	void setTurnTableRunningStateManager(TurnTableRunningStateManager* turnTableRunningStateManager);
	TurnTableCodec* getTurnTableCodec();
	void setTurnTableCodec(TurnTableCodec* turnTableCodec);
	MEMSDataCodec* getMEMSDataCodec();
	void setMEMSDataCodec(MEMSDataCodec* mEMSDataCodec);
	MEMSDataHandler* getMEMSDataHandler();
	void setMEMSDataHandler(MEMSDataHandler* mEMSDataHandler);
	MEMSDataSaver* getMEMSDataSaver();
	void setMEMSDataSaver(MEMSDataSaver* mEMSDataSaver);
	ScriptRunner* getScriptRunner();
	void setScriptRunner(ScriptRunner* scriptRunner);
	ScriptSaveLoader* getScriptSaveLoader();
	void setScriptSaveLoader(ScriptSaveLoader* scriptSaveLoader);
	SerialPortManager* getSerialPortManager_turnTable();
	void setSerialPortManager_turnTable(SerialPortManager* serialPortManager_turnTable);
	SerialPortManager* getSerialPortManager_mEMSData();
	void setSerialPortManager_mEMSData(SerialPortManager* serialPortManager_mEMSData);
	SetTurnTableAndThermostatDialog* getSetTurnTableAndThermostatDialog();
	void setSetTurnTableAndThermostatDialog(SetTurnTableAndThermostatDialog* setTurnTableAndThermostatDialog);
	SetMEMSDataDialog* getSetMEMSDataDialog();
	void setSetMEMSDataDialog(SetMEMSDataDialog* setMEMSDataDialog);
	SetScriptDialog* getSetScriptDialog();
	void setSetScriptDialog(SetScriptDialog* setScriptDialog);
	ParameterConsole* getParameterConsole();
	void setParameterConsole(ParameterConsole* parameterConsole);

public:
	virtual void objectAutoInjection(QHash<QString, QObject*>* objMap)override;
};
